<?php

namespace app\admin\controller;
use app\admin\model\ActiveCases;
use app\admin\model\ActiveCasesType;
use think\Request;

class ActiveCase extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }

    /**
     * @return mixed
     * 活动案例列表
     */
    public function activeCaseList(){
        $activeCases = new ActiveCases();
        $where['id'] = ['neq','0'];
        $rs = $activeCases->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    /**
     * @param Request $request
     * @return mixed|void
     *  添加活动案例
     */
    public function addActiveCase(Request $request){
        if($request->post()){
            $activeCases = new ActiveCases();
            $data = $request->post();
            if($data["type"] == '' || $data["name"] == '' || $data["index_sort"] == '' || $data["img_url"] == '' || $data["index_status"] == '' || $data["status"] == ''){
                return $this->error('参数不完整');
            }
            $rs = $activeCases->add($data);
            if($rs){
                return $this->success('添加成功','admin/ActiveCase/activeCaseList');
            }else{
                return $this->error('添加失败');
            }
        }
        $activeCasesType = new ActiveCasesType();
        $where['id'] = ['neq',0];
        $rs = $activeCasesType->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    /**
     * @param Request $request
     * @return mixed|void
     * 修改活动案例
     */
    public function editActiveCase(Request $request){
        $activeCases = new ActiveCases();
        if($request->post()){
            $data = $request->post();
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $activeCases->edit($where,$data);
            if($rs){
                return $this->success('修改成功','admin/ActiveCase/activeCaseList');
            }
            return $this->error('修改失败');
        }else{
            $where['id'] = $request->get('id');
            $rs = $activeCases->detailJoin($where);
        }

        $activeCasesType = new ActiveCasesType();
        $where['id'] = ['neq',0];
        $type = $activeCasesType->getAll($where);

        $this->assign('rs',$rs);
        $this->assign('type',$type);
        return $this->fetch();
    }

    /**
     * @param Request $request
     * 删除活动案例
     */

    public function delActiveCase(Request $request){
        $activeCases = new ActiveCases();
        $where['id'] = $request->get('id');
        $rs = $activeCases->del($where);
        if($rs){
            return $this->success('删除成功','admin/ActiveCase/activeCaseList');
        }
        return $this->error('删除失败');
    }
}